package com.jeecms.common.upload;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Hashtable;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.multipart.MultipartFile;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

/**
 * 本地文件存储
 */
public class FileRepository implements ServletContextAware {
	private Logger log = LoggerFactory.getLogger(FileRepository.class);

	public String storeByExt(String path, String ext, MultipartFile file,String shopJc)
			throws IOException {
		String filename = UploadUtils.generateFilename(path, ext,shopJc);
		File dest = new File(ctx.getRealPath(filename));
		dest = UploadUtils.getUniqueFile(dest);
		store(file, dest);
		return filename;
	}

	public String storeByFilename(String filename, MultipartFile file)
			throws IOException {
		File dest = new File(ctx.getRealPath(filename));
		store(file, dest);
		return filename;
	}

	public String storeByExt(String path, String ext, File file , String shopJc)
			throws IOException {
		String filename = UploadUtils.generateFilename(path, ext, shopJc);
		File dest = new File(ctx.getRealPath(filename));
		dest = UploadUtils.getUniqueFile(dest);
		store(file, dest);
		return filename;
	}

	public String storeByFilename(String filename, File file)
			throws IOException {
		File dest = new File(ctx.getRealPath(filename));
		store(file, dest);
		return filename;
	}

	private void store(MultipartFile file, File dest) throws IOException {
		try {
			UploadUtils.checkDirAndCreate(dest.getParentFile());
			file.transferTo(dest);
		} catch (IOException e) {
			log.error("Transfer file error when upload file", e);
			throw e;
		}
	}

	private void store(File file, File dest) throws IOException {
		try {
			UploadUtils.checkDirAndCreate(dest.getParentFile());
			FileUtils.copyFile(file, dest);
		} catch (IOException e) {
			log.error("Transfer file error when upload file", e);
			throw e;
		}
	}

	public File retrieve(String name) {
		return new File(ctx.getRealPath(name));
	}

	public  String CreateQR(String path,String fileName,String ext,String content) {
		
		if (StringUtils.isEmpty(path) || StringUtils.isEmpty(fileName) || StringUtils.isEmpty(ext) ||
				StringUtils.isEmpty(content) ) {
			return StringUtils.EMPTY;
		}
		Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
		hints.put(EncodeHintType.CHARACTER_SET, "GBK");
		BitMatrix matrix = null;
		try {
			matrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE, 300, 300, hints);
		} catch (WriterException e) {
			e.printStackTrace();
		}
		if(path.length() > path.lastIndexOf('/')+1){
			path = path + "/";
		}
		String filePathStr = path+ UploadUtils.YEAR_MONTH_FORMAT.format(new Date());
		File pathDir = new File(ctx.getRealPath(filePathStr)) ;
		UploadUtils.checkDirAndCreate(pathDir);
		String t_fileName = UploadUtils.SHORT_FORMAT.format(new Date())+fileName+"."+ext;
		File file = new File(pathDir.getPath()+"/"+t_fileName);
		try {
			MatrixToImageWriter.writeToFile(matrix, ext, file);
		} catch (IOException e) {
			e.printStackTrace();
			file = null;
		}finally{
			if (null != file) {
				return filePathStr+"/"+t_fileName;
			}else {
				return "";
			}
		}
	}
	private ServletContext ctx;

	public void setServletContext(ServletContext servletContext) {
		this.ctx = servletContext;
	}
}
